package com.pmcc.utils.wechat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.pmcc.utils.DateUtil;
import com.pmcc.utils.StringUtil;

import com.pmcc.utils.wechat.*;
import com.qiniu.storage.model.DefaultPutRet;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName WechatServiceMessageUtils
 * @Author chenqf
 * @Description //TODO
 * @Date 2019/8/7 0:00
 * @Version 1.0
 */
@Slf4j
public class WechatServiceMessageUtils {




    /**
     * 公众号发送文本消息 加小程序
     * @param openId 用户openId
     * @param shopId 推送的店铺编号
     * @param values 推送的内容  可以不传
     * @return
     */
    public static String sendMessageByOfficialAccounts(String openId,String shopId,String values,String accessToken){
        String miniAppId = WechatMiniConfig.MINI_APP_ID;
        String content = "";
        if(StringUtil.isNotEmpty(values)){
            content =values+"\r\n<a href=\"http://www.qq.com\" data-miniprogram-appid="+miniAppId+" data-miniprogram-path=\"/pages/index/index?shopId="+shopId+"&type=1\">点击跳小程序</a>";
        }else{
            content ="<a href=\"http://www.qq.com\" data-miniprogram-appid="+miniAppId+" data-miniprogram-path=\"/pages/index/index?shopId="+shopId+"&type=1\">点击跳小程序</a>";
        }
        String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("content",content);
        jsonObject.put("touser",openId);
        jsonObject.put("msgtype","text");
        jsonObject.put("text",jsonObject1);
        String json = HttpClientUtil.httpPostWithJson(sendUrl, jsonObject);
        return json;
    }
    /**
     * 公众号发送文本消息
     * @param openId 用户openId
     * @param content 推送的内容
     * @return
     */
    public static String sendMessageText(String openId,String content,String accessToken){
        String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("content",content);
        jsonObject.put("touser",openId);
        jsonObject.put("msgtype","text");
        jsonObject.put("text",jsonObject1);
        String json = HttpClientUtil.httpPostWithJson(sendUrl, jsonObject);
        log.info("公众号发送文本消息"+json);
        return json;
    }



    /**
     * 发送客服消息  视频消息发送
     * @param accessToken
     * @param openId
     * @param mediaId
     * @param thumbMediaId
     * @param title
     * @param description
     * @return
     */
    public static String sendMessageVideo(String accessToken,String openId,String mediaId,String thumbMediaId,String title,String description){

        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        JSONObject videoObject = new JSONObject();
        videoObject.put("media_id",mediaId);
        videoObject.put("thumb_media_id",thumbMediaId);
        videoObject.put("title",title);
        videoObject.put("description",description);

        jsonObject.put("touser",openId);
        jsonObject.put("msgtype","video");
        jsonObject.put("video",videoObject);
        String json = HttpClientUtil.httpPostWithJson(url, jsonObject);
        log.info("发送客服消息  视频消息发送"+json+title);
        return json;
    }

    /*public static void main(String[] args) {
        String accessToken = getAccessToken();
        System.out.println(accessToken);
        //String accessToken = "36_1Yn_F1BmUH1XOIJEDsR5raCOKVkc7lVQqB44wAJW7QdNLQcs-0zB2UE1Bb8vsiBLMc7BM80J01-05-jQXEX2R1rVnIft66gvSKJR6K0ohASKdNWsUgY5WXEwIkhgCGpHj5BxatrR9LFZFHtdSUDhAIAWYH";
        String s = sendMessageVideo(accessToken, "o61sy1JJtKalabBT-hShyTpzjwNs", "xhc1u54zG8mu7Ls3LTiZZWV7zHl68SAWT_rfKICICnI", "Nab3iOszH0OXVF_RLzKMVXzQ4kDePDIpmh6EU3BbuCy0l0N8s_nbc5w-0Zi9rLag", "我是一个测试视频", "我是一个测试视频");
        log.info(s);
    }*/


    /**
     *  公众号给用户发送图文消息
     * @param openId 用户openId
     * @param title 标题
     * @param description 内容
     * @param url 跳转链接
     * @param picurl 图片链接
     * @return
     */
    public static String sendMessageByOfficialAccountsImgText(String openId,String title,String description,String url,String picurl ,String accessToken){
        /*String accessToken = getAccessToken();*/
        String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonObject2.put("title",title);
        jsonObject2.put("description",description);
        jsonObject2.put("url",url);
        jsonObject2.put("picurl",picurl);
        jsonArray.add(jsonObject2);
        jsonObject1.put("articles",jsonArray);
        jsonObject.put("touser",openId);
        jsonObject.put("msgtype","news");
        jsonObject.put("news",jsonObject1);
        String json = HttpClientUtil.httpPostWithJson(sendUrl, jsonObject);
        return json;
    }

    /**
     *  客服消息   发送图片
     * @param openId 用户openId
     * @param mediaId 标题
     * @return
     */
    public static String sendMessageByImage(String openId,String mediaId,String accessToken){
        /*String accessToken = getAccessToken();*/
        String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("media_id",mediaId);
        jsonObject.put("touser",openId);
        jsonObject.put("msgtype","image");
        jsonObject.put("image",jsonObject2);
        String json = HttpClientUtil.httpPostWithJson(sendUrl, jsonObject);
        return json;
    }

    /*public static void main(String[] args) {
        *//*String accessToken = getAccessToken();
        System.out.println(accessToken);*//*
        //{"media_id":"xhc1u54zG8mu7Ls3LTiZZfB8_6ubt51MG7uBoHT53ZU","url":"http:\/\/mmbiz.qpic.cn\/sz_mmbiz_jpg\/D6T5ibvaBShPsibNEWiceIe5fY2Z28ZWHTFG9wQYDicXnQZ5qPRSZLZib2RchprROibNbXL8Gu15CiaP0LQBHMn0MPZYA\/0?wx_fmt=jpeg","item":[]}
        sendMessageByImage("o61sy1JJtKalabBT-hShyTpzjwNs","xhc1u54zG8mu7Ls3LTiZZQ9fuF5zocXuUJDOLdYhOAY","36_j_qfvdR3Et74XnA3ua8HwyOVLQlg0WGT4aTgcruBRSt-skHN4bCj8MEAc7Sff3jftyMsY9NJ7bDQUkkjrjETEZst3STFg2wxBkWNKiggz1_QEGkT8Y-seHM3vGZgaUz1FssMxBbHSgaCdm3hPRKfAEAEGZ");
    }*/



    /**
     * @Fly通过用户公众号给用户推送模板消息
     * @param shopId
     * @param openId
     * @param templateId
     * @param url
     * @param keywordMap
     * @return
     */
    public static String sendMessageToShop(String shopId,String openId,String templateId,String url,String miniUrl,Map<String,String> keywordMap,Map<String,String> colorMap,String accessToken){
        JSONObject data = new JSONObject();
        if(keywordMap != null){
            Set<String> keywordSet = keywordMap.keySet();
            for(String keywords:keywordSet){
                String value = keywordMap.get(keywords);
                if(StringUtil.isNotEmpty(value)){
                    JSONObject keyword = new JSONObject();
                    keyword.put("value",value);
                    //keyword.put("color","#173177");
                    keyword.put("color",colorMap.get(keywords));
                    data.put(keywords,keyword);
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser",openId);
        jsonObject.put("template_id",templateId);
        String miniAppId = WechatMiniConfig.MINI_APP_ID;
        if(StringUtil.isNotEmpty(url)){
            jsonObject.put("url",url);   //模板跳转链接（海外帐号没有跳转能力）非必传参数
        }
        JSONObject miniprogram = new JSONObject();
        if(StringUtil.isNotEmpty(miniUrl)){
            miniprogram.put("appid",miniAppId);
            /*miniprogram.put("pagepath","/pages/index/index?shopId="+shopId+"&type=1");*/
            miniprogram.put("pagepath",miniUrl);
            jsonObject.put("miniprogram",miniprogram);
        }
        jsonObject.put("data",data);
        String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
        System.out.println(jsonObject);
        String json = HttpClientUtil.httpPostWithJson(sendUrl, jsonObject);
        return json;
    }





 /*   public static void main(String[] args) {
        String accessToken = getAccessToken();
        System.out.println(accessToken);
    }
*/


}
